package com.danielepelagatti.utils
{
	import com.adobe.net.DynamicURLLoader;
	import com.adobe.webapis.ServiceBase;
	import com.adobe.webapis.URLLoaderBase;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.utils.UIDUtil;
	import flash.net.URLLoader;
	/**
	 * 
	 * @author dpelagatti
	 * 
	 */
	public class URLLoaderBaseTracker extends EventDispatcher
	{
		public static const LOADING_ACTIVITY:String 	= "loading_activity";
		public static const LOADING_PROGRESS:String		= "loading_progress";
		public static const LOADING_INACTIVITY:String 	= "loading_inactivity";
		/**
		* 
		*/		
		protected static var instance:URLLoaderBaseTracker;
		/**
		* 
		*/		
		//protected static var totalPendingOperations:int;
		/**
		* 
		*/		
		protected static var pendingOperations:ArrayCollection = new ArrayCollection();
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public static function getInstance():URLLoaderBaseTracker
		{
			if(!instance)
				instance = new URLLoaderBaseTracker();
			return instance;
		}
		/**
		 * 
		 * @param type
		 * 
		 */		
		public function addURLLoader(urlLoader:URLLoaderBase,eventType:String):void
		{
			pendingOperations.addItem(urlLoader);
			urlLoader.addEventListener(ProgressEvent.PROGRESS,onProgress);
			urlLoader.addEventListener(eventType,onComplete);
			dispatchEvent(new Event(LOADING_ACTIVITY));
		}
		protected var bytesTotalPerURLLoader:Dictionary = new Dictionary();
		protected var bytesLoadedPerURLLoader:Dictionary = new Dictionary();
		/**
		 * 
		 * @param event
		 * 
		 */		
		public function onProgress(event:ProgressEvent):void
		{
			for each(var urlLoader:URLLoaderBase in pendingOperations)
			{
				if(urlLoader == event.currentTarget)
				{
					var loaderId:String = UIDUtil.getUID(urlLoader);
					
					if(event.bytesLoaded != event.bytesTotal)
					{
						bytesLoadedPerURLLoader[loaderId] = event.bytesLoaded;
						bytesTotalPerURLLoader[loaderId] = event.bytesTotal;
					}
					else
					{
						if(bytesLoadedPerURLLoader[loaderId])
							delete bytesLoadedPerURLLoader[loaderId];
						if(bytesTotalPerURLLoader[loaderId])
							delete bytesTotalPerURLLoader[loaderId];					
					}
				}
			}
			dispatchEvent(new Event(LOADING_PROGRESS));
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onComplete(event:Event):void
		{
			event.currentTarget.removeEventListener(event.type,onComplete);
			var index:int = pendingOperations.getItemIndex(event.currentTarget);
			if(index != -1)
			{
				pendingOperations.removeItemAt(index);
			}
			else
				trace("error");
			
			if(pendingOperations.length == 0)dispatchEvent(new Event(LOADING_INACTIVITY));
		}
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get totalOperations():int
		{
			return pendingOperations.length;
		}
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get cumulativeBytesTotal():Number
		{
			var t:Number = 0;
			for (var i:String in bytesTotalPerURLLoader)
			{
				t += Number( bytesTotalPerURLLoader[i] );
			}
			return t;
		}
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get cumulativeBytesLoaded():Number
		{
			var t:Number = 0;
			for (var i:String in bytesLoadedPerURLLoader)
			{
				t += Number( bytesLoadedPerURLLoader[i] );
			}
			return t;
		}
	}
}